import javax.microedition.lcdui.StringItem;

public class LetterString extends StringItem {

	private String hiddenWord;
	
	public LetterString(String label) {
		super(label, "");
	}
	
	public LetterString(String label, String hiddenWord) {
		super(label, "");
		
		this.hiddenWord = hiddenWord;
		
		StringBuffer sb = new StringBuffer("");
		for (byte i = 0; i < hiddenWord.length(); i++) {
			sb.append('_');
		}
		this.setText(sb.toString());
	}

	public boolean addLetter(char c) {
		for (byte i = 0; i < this.getText().length(); i += 2) {
			if (this.getText().charAt(i) == Character.toUpperCase(c)) {
				return true;
			}
		}
		
		this.setText(this.getText() + Character.toUpperCase(c) + " ");
		return false;
	}

	public boolean testLetter(char c) {
		boolean b = true;
		StringBuffer sb = new StringBuffer(this.getText());
		
		for (byte i = 0; i < this.hiddenWord.length(); i ++) {
			if (this.hiddenWord.charAt(i) == Character.toUpperCase(c)) {
				sb.deleteCharAt(i);
				sb.insert(i, Character.toUpperCase(c));
				b = false;
			}
		}
		this.setText(sb.toString());
		
		return b;
	}

	public boolean hidden() {
		return this.getText().indexOf("_") != -1;
	}
}
